package com.jc.oms.controller.permission;

import com.jc.common.util.WebUtils;
import com.jc.ds.base.service.BaseServiceException;
import com.jc.ds.model.Permission;
import com.jc.oms.controller.BaseController;
import com.jc.service.permission.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("all")
@Controller
@RequestMapping("permission")
public class PermissionController extends BaseController {

    @Autowired
    private PermissionService permissionService;


    @RequestMapping(value = "/queryPermissionByRoleId",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryPermissionByRoleId(HttpServletRequest request){
            Map<String,Object> resultMap = new HashMap<String, Object>(){{
                put("code",10000);
                put("data",null);
            }};
            Map<String,Object> param = null;
            try{
                param = WebUtils.ReadAsChars(request);
                if (param.get("roleId") == null){
                    throw new BaseServiceException("必传参数为空!!! roleId");
                }
                List<Permission> data = permissionService.queryPermissionByRoleId(Long.valueOf(param.get("roleId").toString()));
                resultMap.put("data",data);
            }catch (Exception e){
                resultMap.put("code",9999);
                resultMap.put("msg",e.getLocalizedMessage());
                e.printStackTrace();
            }
            return resultMap;
    }

    @RequestMapping(value = "/updateRolePermission",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateRolePermission(HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>() {{
            put("code", 10000);
            put("data", null);
        }};
        Map<String, Object> param = null;
        String token = null;
        try {
            param = WebUtils.ReadAsChars(request);
            if (param.get("roleId") == null || param.get("permissionId") == null || param.get("isChecked") == null) {
                throw new BaseServiceException("必传参数为空!!!");
            }
            Boolean  data = permissionService.updateRolePermission(param.get("roleId").toString(),param.get("permissionId").toString(),param.get("isChecked").toString());
            resultMap.put("data", data);
        } catch (Exception e) {
            resultMap.put("code", 9999);
            resultMap.put("msg", e.getLocalizedMessage());
            e.printStackTrace();
        }
        return resultMap;
    }
}
